package dado;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;


import organizador.Emprestimo;



public class DAOEmprestimo  extends DAODefault{
	
	
	public void createEmprestimo(Emprestimo emprestimo){
        String sql = "insert into emprestimo (dataEmprestimo,dataDevolucao,cpfBibliotecaria,cpfUser,codigo,devolvido) values(?,?,?,?,?,?)";
       
        try{
           
            stmt = conn.prepareStatement(sql);
           
            stmt.setDate(1,new java.sql.Date(emprestimo.getDataEmprestimo().getTimeInMillis()));
            stmt.setDate(2,new java.sql.Date(emprestimo.getDataDevolucao().getTimeInMillis()));
            stmt.setString(3,emprestimo.getCpfBibliotecaria());
            stmt.setString(4,emprestimo.getCpfUser());
            stmt.setString(5,emprestimo.getCodigo());
            stmt.setBoolean(6,emprestimo.isDevolvido());
            
            stmt.execute();
           
            stmt.close();
           
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

	public Emprestimo searchEmprestimo(String cpf) {

		try {
			Emprestimo emprestimo = new Emprestimo();
			
			String sql = "select * from emprestimo where cpfUser = ? and devolvido = false";

			stmt = conn.prepareStatement(sql);
						
			stmt.setString(1, cpf);
		
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				
				emprestimo.setCodigo(rs.getString("codigo"));
				emprestimo.setCpfBibliotecaria(rs.getString("cpfBibliotecaria" ));
				emprestimo.setCpfUser(rs.getString("cpfUser"));
				emprestimo.setIdEmprestimo(rs.getInt("idEmprestimo"));
				Calendar data = Calendar.getInstance();
				data.setTime(rs.getDate("dataDevolucao"));
				emprestimo.setDataDevolucao(data);
				data.setTime(rs.getDate("dataEmprestimo"));
				emprestimo.setDataEmprestimo(data);
				emprestimo.setDevolvido(rs.getBoolean("devolvido"));
				

			}
			return emprestimo;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	public void updateEmprestimo(Emprestimo emprestimo) {

		String sql = "UPDATE emprestimo SET " + "dataEmprestimo = ?,dataDevolucao = ?,cpfBibliotecaria = ?,cpfUser = ?,devolvido = ? "
				+ "where codigo = ? and idEmprestimo = ?";

		try {
			stmt = conn.prepareStatement(sql);

			stmt.setDate(1,new Date( emprestimo.getDataEmprestimo().getTimeInMillis()));
			stmt.setDate(2,new Date( emprestimo.getDataDevolucao().getTimeInMillis()));
			stmt.setString(3, emprestimo.getCpfBibliotecaria());
			stmt.setString(4, emprestimo.getCpfUser());
			stmt.setBoolean(5, emprestimo.isDevolvido());
			stmt.setString(6, emprestimo.getCodigo());
			stmt.setInt(7, emprestimo.getIdEmprestimo());
			
			
			

			stmt.execute();

			stmt.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		}

	}
	public Boolean disponivelEmprestimo(String cpf) {

		try {
			
			
			String sql = "select * from emprestimo where cpfUser = ? and devolvido = false";

			stmt = conn.prepareStatement(sql);
						
			stmt.setString(1, cpf);
		
			ResultSet rs = stmt.executeQuery();
			if(rs.next()) 
				return false;
			else 
				return true;
				

			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}
